import * as cc from 'cc';
import { TrackingBullet } from './TrackingBullet';
const { ccclass, property } = cc._decorator;
@ccclass('HelloWorld')
export class HelloWorld extends cc.Component {
    @property({ type: cc.Prefab })
    bulletPrefab: cc.Prefab = null;
    @property({ type: cc.Prefab })
    blastPrefab: cc.Prefab = null;
    enemyNode: cc.Node = null;
    heroNode: cc.Node = null;
    start() {
        cc.profiler.hideStats();
        this.enemyNode = this.node.getChildByName('Enemy');
        this.heroNode = this.node.getChildByName('Hero');
        //主角可拖拽
        this.heroNode.on(cc.NodeEventType.TOUCH_MOVE, (event: cc.EventTouch) => {
            let delta = event.getUIDelta();
            let pos = this.heroNode.position;
            this.heroNode.setPosition(pos.x + delta.x, pos.y + delta.y);
        }, this);
        //定时发射追踪导弹
        this.schedule(() => {
            let bulletNode = cc.instantiate(this.bulletPrefab);
            bulletNode.setParent(this.node);
            bulletNode.getComponent(TrackingBullet).init(this.enemyNode.position, this.heroNode);
            //子弹爆炸的回调逻辑
            bulletNode.on('Blast', (pos: cc.Vec3) => {
                let blastNode = cc.instantiate(this.blastPrefab);
                blastNode.setParent(this.node);
                blastNode.setPosition(pos);
                blastNode.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, () => {
                    blastNode.removeFromParent();
                    blastNode.destroy();
                }, this);
                bulletNode.removeFromParent();
                bulletNode.destroy();
            }, this);
        }, 2);
    }
}